﻿<Window x:Class="ITG.MedcinEditor.MedcinEditorWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:ITG.MedcinEditor"
        xmlns:common="clr-namespace:ITG.Common;assembly=ITG.Common"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Height="600" Width="800"
        x:Name="me"
        Title="{Binding Path=WindowTitle}" Closing="me_Closing">

    <Window.Resources>
        <Style x:Key="DirtyFlag" TargetType="Ellipse">
            <Setter Property="Height" Value="12" />
            <Setter Property="Width" Value="12" />
            <Setter Property="Fill" Value="Transparent" />
            <Setter Property="Stroke" Value="Transparent" />
            <Setter Property="StrokeThickness" Value="0" />
            <Setter Property="ToolTip" Value="" />
            <Setter Property="Visibility" Value="Collapsed" />
            
            <Style.Triggers>
                <DataTrigger Binding="{BindTo Dirty}" Value="True">
                    <Setter Property="Visibility" Value="Visible" />
                    <Setter Property="Fill" Value="Red" />
                    <Setter Property="ToolTip" Value="Unsaved changes" />
                </DataTrigger>
            </Style.Triggers>
        </Style>


        <!-- Provides databinding access to Category enum
             Requires: xmlns:System="clr-namespace:System;assembly=mscorlib"-->
        <ObjectDataProvider x:Key="EnumDataProvider"
                            MethodName="GetValues" 
                            ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="common:Category"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

        

    </Window.Resources>
    
    <Window.CommandBindings>
        <CommandBinding Command="Close" Executed="CloseOnExecuted" />
        <CommandBinding Command="Help" Executed="HelpOnExecuted" />
    </Window.CommandBindings>
   
    <Window.InputBindings>
        <KeyBinding Key="N" Modifiers="Control"  Command="{BindTo NewDatabaseCommand}" />
        <KeyBinding Key="O" Modifiers="Control"  Command="{BindTo OpenDatabaseCommand}" />
        <KeyBinding Key="S" Modifiers="Control"  Command="{BindTo SaveDatabaseCommand}" />
        <KeyBinding Key="S" Modifiers="Control+Shift"  Command="{BindTo SaveAsDatabaseCommand}" />
    </Window.InputBindings>

    <Grid x:Name="LayoutRoot" 
          MouseLeftButtonUp="LayoutRoot_OnMouseLeftButtonUp"
          MouseMove="LayoutRoot_OnMouseMove">

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Menu Grid.Row="0">
            <MenuItem Header="_File" >
                <MenuItem Header="_New" InputGestureText="Ctrl+N"
                          Command="{BindTo NewDatabaseCommand}" />
                <MenuItem Header="_Open ..." InputGestureText="Ctrl+O"
                          Command="{BindTo OpenDatabaseCommand}" />
                <MenuItem Header="_Save" InputGestureText="Ctrl+S" 
                          Command="{BindTo SaveDatabaseCommand}" />
                <MenuItem Header="Save _As ..." InputGestureText="Ctrl+Shift+S" 
                          Command="{BindTo SaveAsDatabaseCommand}" />
                <Separator />
                <MenuItem Header="E_xit" InputGestureText="Alt+F4"
                          Command="Close" />
            </MenuItem>
            
            <!--<MenuItem Header="_Edit">
                <MenuItem Header="Reset Sort"
                          Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=ResetSortCommand}" />
            </MenuItem>
            
            <MenuItem Header="_Tools">
                <MenuItem Header="_Validate Database" Command="{BindTo ValidateDatabaseCommand}" />
                <Separator />
                <MenuItem Header="Validate Unique Identifiers" Command="{BindTo ValidateMedcinUIdCommand}"/>
            </MenuItem>-->
            
            <MenuItem Header="_Help" >
                <MenuItem Header="_Help" Command="Help"/>
                <Separator/>
                <MenuItem Header="_About ITG Medcin Term Editor ..." />
            </MenuItem>
        </Menu>

        

        <!-- main content area -->
        <DataGrid x:Name="TermsGrid" Grid.Row="1"
                  ItemsSource="{BindTo Terms}" 
                  AutoGenerateColumns="False" 
                  SelectionMode="Single"
                  CanUserDeleteRows="True"
                  CanUserAddRows="True"
                  CanUserReorderColumns="False"
                  CanUserSortColumns="False"
                  Margin="5 2 5 0"
                  
                  SourceUpdated="TermsGrid_SourceUpdated" 
                  AddingNewItem="TermsGrid_AddingNewItem" 
                  PreparingCellForEdit="TermsGrid_PreparingCellForEdit" 
                  BeginningEdit="TermsGrid_BeginningEdit" 
                  CellEditEnding="TermsGrid_CellEditEnding" 
                  RowEditEnding="TermsGrid_RowEditEnding" 
                  PreviewMouseLeftButtonDown="TermsGrid_OnMouseLeftButtonDown" 
                  ColumnReordered="TermsGrid_ColumnReordered" 
                  Sorting="TermsGrid_Sorting">
            
            <DataGrid.Resources>
                <Style x:Key="NotAnIntegerValidationStyle" TargetType="{x:Type TextBox}">
                    <Setter Property="Padding" Value="-2"/>
                    <Style.Triggers>
                        <Trigger Property="Validation.HasError" Value="True">
                            <Setter Property="Background" Value="Yellow" />
                            <Setter Property="ToolTip" 
                                    Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
            
            <!--<DataGrid.InputBindings>
                <KeyBinding Key="Enter" Command="{Binding AdvanceRowCommand}" />
                <KeyBinding Key="Enter" Modifiers="Control" Command="{....whatever the default Enter press command is ....}" />
                <KeyBinding Key="Delete" Command="{Binding ElementName=me, Path=ClearCurrentCellCommand}" />
                <KeyBinding Key="Delete" Modifiers="Shift" Command="Delete" />
            </DataGrid.InputBindings>-->
            
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Key, Mode=OneWay, NotifyOnSourceUpdated=True, ValidatesOnExceptions=True}">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="Key" ToolTipService.ToolTip="{x:Static common:MedcinTerm.KeyToolTip}"/>
                    </DataGridTextColumn.Header>
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Setter Property="FontStyle" Value="Italic" />
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>


                <DataGridTextColumn Binding="{Binding MedcinCode, Mode=TwoWay, NotifyOnSourceUpdated=True, ValidatesOnExceptions=True}"
                                    EditingElementStyle="{StaticResource NotAnIntegerValidationStyle}">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="Code" 
                                   ToolTipService.ToolTip="{x:Static common:MedcinTerm.MedcinCodeToolTip}"/>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Prefix, Mode=TwoWay, NotifyOnSourceUpdated=True}" >
                    <DataGridTextColumn.Header>
                        <TextBlock Text="Prefix" 
                                   ToolTipService.ToolTip="{x:Static common:MedcinTerm.PrefixToolTip}"/>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding DefaultLabel, Mode=TwoWay, NotifyOnSourceUpdated=True}"
                                    Width="*">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="Default Label" 
                                   ToolTipService.ToolTip="{x:Static common:MedcinTerm.DefaultLabelToolTip}"/>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding UserLabel, Mode=TwoWay, NotifyOnSourceUpdated=True}" 
                                    Width="*">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="Custom Label" 
                                   ToolTipService.ToolTip="{x:Static common:MedcinTerm.UserLabelToolTip}"/>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTemplateColumn SortMemberPath="Category">
                    <DataGridTemplateColumn.Header>
                        <TextBlock Text="Category" 
                                   ToolTipService.ToolTip="{x:Static common:MedcinTerm.CategoryToolTip}"/>
                    </DataGridTemplateColumn.Header>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox HorizontalAlignment="Stretch" VerticalAlignment="Center"
                                      ItemsSource="{Binding Source={StaticResource EnumDataProvider}}" 
                                      SelectedItem="{Binding Category, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding StateOneText, Mode=TwoWay, NotifyOnSourceUpdated=True}"
                                    Width="*">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="Text for state one" 
                                   ToolTipService.ToolTip="{x:Static common:MedcinTerm.StateOneTextToolTip}"/>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding StateTwoText, Mode=TwoWay, NotifyOnSourceUpdated=True}"
                                    Width="*">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="Text for state two" 
                                   ToolTipService.ToolTip="{x:Static common:MedcinTerm.StateTwoTextToolTip}"/>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTemplateColumn SortMemberPath="StateOneIsPositiveFinding" Width="30">
                    <DataGridTemplateColumn.Header>
                        <TextBlock Text="Pos" 
                                   ToolTipService.ToolTip="{x:Static common:MedcinTerm.StateOneIsPositiveFindingToolTip}"/>
                    </DataGridTemplateColumn.Header>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" 
                                      IsChecked="{Binding StateOneIsPositiveFinding, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <!-- the popup that is displayed if user moves rows -->
        <Popup x:Name="DragDropPopup"
               IsHitTestVisible="False"
               Placement="RelativePoint"
               PlacementTarget="{Binding ElementName=me}"
               AllowsTransparency="True">
            <Border BorderBrush="LightSteelBlue"
                    BorderThickness="2"
                    Background="White"
                    Opacity="0.75">
                <StackPanel Orientation="Horizontal" Margin="4,3,8,3">
                    <Rectangle Fill="Yellow" Width="16" Height="16" />
                    
                    <!--<Image Source="/Shared/Images/DragInsert.png"
                           Width="16" Height="16" />-->
                    <TextBlock FontSize="14"
                               FontWeight="Bold"
                               VerticalAlignment="Center"
                               Text="{Binding ElementName=me, Path=DraggedItem}"
                               Margin="8,0,0,0" />
                </StackPanel>
            </Border>
        </Popup>
        

        <StatusBar Grid.Row="3">
            <!-- Left-aligned StatusBar stuff -->
            <!--<TextBlock Text="Status: " Margin="0 0 5 0" />
            <TextBlock Name="StatusTextBlock" Margin="0 0 5 0" />-->
            
            <!-- Right-aligned StatusBar stuff -->
            <!--<StatusBarItem DockPanel.Dock="Right" Margin="5 0 10 0">
                <Ellipse Style="{StaticResource DirtyFlag}" />
            </StatusBarItem>-->
            <StatusBarItem DockPanel.Dock="Right" Margin="0 0 5 0">
                <TextBlock Text="{BindTo CurrentFilePath}" />
            </StatusBarItem>

            <!-- Empty filler for the DockPanel; needs to be last -->
            <StatusBarItem /> 
            
        </StatusBar>

    </Grid>
</Window>
